#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 110;
int n, m;
struct S
{
	string name;
	int sum;
	int id;
}s[N];
int fun(string s)
{
	int sum = 0;
	for (int i = 0; i < s.size() - 1; i++) {
		for (int j = i + 1; j < s.size(); j++) {
			if (s[i] > s[j]) {
				sum++;
			}
		}
	}
	return sum;
}
bool cmp(S a, S b)
{
	if (a.sum != b.sum) {
		return a.sum < b.sum;
	}
	else {
		return a.id < b.id;
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		string a;
		cin >> a;
		s[i].id = i + 1;
		s[i].name = a;
		s[i].sum = fun(a);
	}
	sort(s, s + m ,cmp);
	for (int i = 0; i < m; i++) {
		cout << s[i].name << endl;
	}
	return 0;
}